﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Resources;
using System.Text;

namespace STCommon.Resources
{
    public class TextResourceManager : ResourceManager
    {
        private TextResourceManager() :
            base("STCommon.Resources.CommonText", typeof(CommonText).Assembly)
        {
        }

        private static TextResourceManager Instance 
        {
            get
            {
                if(m_Instance == null)
                {
                    m_Instance =new TextResourceManager();
                }

                return m_Instance;
            }

        }

        public static string DoubleRangeValidationText
        {
            get
            {
                return Instance.GetString("DoubleRangeValidationText", CommonText.Culture);
            }
        }

        /// <summary>
        ///   Looks up a localized string similar to Value must be an integer between {0} and {1}.
        /// </summary>
        public static string IntegerRangeValidationText
        {
            get
            {
                return Instance.GetString("IntegerRangeValidationText", CommonText.Culture);
            }
        }

        /// <summary>
        ///   Looks up a localized string similar to Value must be a duration between {0:00}:{1:00} and {2:00}:{3:00}.
        /// </summary>
        public static string TimeRangeValidationText
        {
            get
            {
                return Instance.GetString("TimeRangeValidationText", CommonText.Culture);
            }
        }

        public static string ValueValidationTitleText
        {
            get
            {
                return Instance.GetString("ValueValidationTitleText", CommonText.Culture);
            }
        }

        public static string DistancePercentText
        {
            get
            {
                return Instance.GetString("DistancePercentText", CommonText.Culture);
            }
        }

        public static string TimePercentText
        {
            get
            {
                return Instance.GetString("TimePercentText", CommonText.Culture);
            }
        }

        public static string CSVFileText
        {
            get
            {
                return Instance.GetString("CSVFileText", CommonText.Culture);
            }
        }

        protected override ResourceSet InternalGetResourceSet(CultureInfo culture, bool createIfNotExists, bool tryParents)
        {
            ResourceSet rs = (ResourceSet)this.ResourceSets[culture];

            if (rs == null)
            {
                Stream store = null;

                // Lazy-load default language;
                if (m_NeutralResourcesCulture == null)
                {
                    m_NeutralResourcesCulture = GetNeutralResourcesLanguage(MainAssembly);
                }

                // If we're asking for the default language, then ask for the invariant
                //  (non-specific) resources.
                if (m_NeutralResourcesCulture.Equals(culture))
                {
                    culture = CultureInfo.InvariantCulture;
                }

                store = MainAssembly.GetManifestResourceStream(GetResourceFileName(culture));

                // If we found the appropriate resources in the local assembly
                if (store != null)
                {
                    rs = new ResourceSet(store);

                    // Save for later.
                    AddResourceSet(this.ResourceSets, culture, ref rs);
                }
            }

            return rs;
        }

        // Rrivate method in framework, had to be re-specified here.
        private static void AddResourceSet(Hashtable localResourceSets, CultureInfo culture, ref ResourceSet rs)
        {
            lock (localResourceSets)
            {
                ResourceSet objA = (ResourceSet)localResourceSets[culture];
                if (objA != null)
                {
                    if (!object.Equals(objA, rs))
                    {
                        rs.Dispose();
                        rs = objA;
                    }
                }
                else
                {
                    localResourceSets.Add(culture, rs);
                }
            }
        }

        private static TextResourceManager m_Instance = null;

        private CultureInfo m_NeutralResourcesCulture = null;
    }
}
